/**
 * 
 */
package com.zhiteng.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhiteng.entity.Procdef;
import com.zhiteng.entity.Procins;
import com.zhiteng.mapper.ProcdefMapper;
import com.zhiteng.mapper.ProcinsMapper;
import com.zhiteng.service.ProcessService;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

/**
 * <p>
 * Title: LeaveProcessServiceImpl.java
 * </p>
 * <p>
 * Description: 流程Service
 * </p>
 * 
 * @author dragon
 * @date 2018年12月3日
 * @version 1.0
 */
@Service
public class ProcessServiceImpl implements ProcessService {
	@Autowired
	private ProcdefMapper procdefMapper;
	@Autowired
	private ProcinsMapper procinsMapper;

	@Override
	// 删除流程定义
	public void delProcdef(String schoolId, Integer type) {
		Procdef procdef = new Procdef();
		procdef.setSchool_id(schoolId);
		procdef.setType(type);
		procdefMapper.delete(procdef);
	}

	@Override
	public int saveProcdefBatch(List<Procdef> pdList) {
		int count = 0;
		for (Procdef procdef : pdList) {
			count += procdefMapper.insertSelective(procdef);
		}
		return count;
	}

	@Override
	// 查询流程定义
	public List<Procdef> queryProcdef(String schoolId, Integer type) {
		Example example = new Example(Procdef.class);
		example.orderBy("sequence").asc();
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("school_id", schoolId);
		criteria.andEqualTo("type", type);

		return procdefMapper.selectByExample(example);
	}

	@Override
	public void saveProcinsBatch(List<Procins> procinsList) {
		for (Procins p : procinsList) {
			procinsMapper.insertSelective(p);
		}
	}

	@Override
	// 一条流程实例对应多个Procins对象（表中多条记录）
	public List<Procins> queryProcinsByBillId(long billId) {
		Example example = new Example(Procins.class);
		example.orderBy("sequence").asc(); // 升序
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("bill_id", billId);
		criteria.andEqualTo("type", 1); // 请假单
		// criteria.andIsNotNull("status");
		return procinsMapper.selectByExample(example);
	}

	@Override
	public List<Procins> queryProcinsByRecord(Procins record) {
		return procinsMapper.select(record);
	}

	@Override
	public void updateProcinsBatch(List<Procins> procinsList) {
		for (Procins procins : procinsList) {
			procinsMapper.updateByPrimaryKeySelective(procins);
		}
	}

	@Override
	public void updateProcinsByRecord(Procins procins) {
		procinsMapper.updateByPrimaryKeySelective(procins);
	}

	@Override
	public List<Procins> queryComplatedProcinsByAssigneeId(String teacherId, String schoolId) {
		Example example = new Example(Procins.class);
		// example.orderBy("end").desc();
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("type", 1);
		criteria.andEqualTo("assignee_id", teacherId);
		criteria.andEqualTo("school_id", schoolId);
		criteria.andIsNotNull("status"); // 已确认
		return procinsMapper.selectByExample(example);
	}

	@Override
	public void deleteProcinsBybillIdAndType(Long billId, int type) {
		Procins procins = new Procins();
		procins.setType(type);
		procins.setBill_id(billId);
		procinsMapper.delete(procins);
	}

	//////////////////////////////////////////////////////////

	@Override
	public int saveProcins(Procins procins) {
		return procinsMapper.insertSelective(procins);
	}

	@Override
	public List<Procdef> queryProcdefBySchoolId(String school_id) {

		Procdef record = new Procdef();
		record.setSchool_id(school_id);
		List<Procdef> list = procdefMapper.select(record);
		// 排序
		Collections.sort(list, new Comparator<Procdef>() {

			@Override
			public int compare(Procdef o1, Procdef o2) {
				Integer l = o1.getSequence();
				Integer l2 = o2.getSequence();
				if (l < l2) {
					return -1;
				}
				if (l == l2) {
					return 0;
				}
				return 1;
			}
		});
		return list;
	}

	@Override
	public int updateProcinsStatus(String schoolId, Long billId, int status, String id, String remark) {

		Procins record = new Procins();
		record.setStatus(status);
		record.setEnd(new Date());
		record.setRemark(remark);
		Example example = new Example(Procins.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("school_id", schoolId);
		criteria.andEqualTo("bill_id", billId);
		criteria.andEqualTo("assignee_id", id);

		int i = procinsMapper.updateByExampleSelective(record, example);
		return i;

	}

	@Override
	public int updateComplated(Long billId) {
		Procins record = new Procins();
		record.setComplated(1);
		Example example = new Example(Procins.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("bill_id", billId);
		return procinsMapper.updateByExampleSelective(record, example);
	}

	@Override
	public List<Procins> queryProcinsBySchoolIdAndAssigneeIdAndBillId(String schoolId, String assigneeId, Long billId) {

		Procins record = new Procins();
		record.setSchool_id(schoolId);
		record.setAssignee_id(assigneeId);
		record.setBill_id(billId);
		return procinsMapper.select(record);
	}

	@Override
	public void setNextStart(int i, String schoolId, Long billId) {
		Procins record = new Procins();
		record.setStart(new Date());
		Example example = new Example(Procins.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("bill_id", billId);
		criteria.andEqualTo("school_id", schoolId);
		criteria.andEqualTo("sequence", i);
		procinsMapper.updateByExampleSelective(record, example);
	}

	@Override
	public List<Procins> queryProcinsByAssigneeId(String assigneeId) {

		Example example = new Example(Procins.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("assignee_id", assigneeId);
		criteria.andEqualTo("type", 2);
		criteria.andIsNull("end");
		criteria.andIsNotNull("start");
		criteria.orEqualTo("type", 3);
		criteria.andEqualTo("assignee_id", assigneeId);
		criteria.andIsNull("end");
		criteria.andIsNotNull("start");
		List<Procins> list3 = procinsMapper.selectByExample(example);

		return list3;
	}

	@Override
	public List<Procins> queryProcinsByAssigneeIdAndStatus(String assignee_id, int i) {
		Procins record = new Procins();
		record.setAssignee_id(assignee_id);
		record.setStatus(i);
		return procinsMapper.select(record);
	}

	@Override
	public int deleteProcins(Long billId) {
		Procins record = new Procins();
		record.setBill_id(billId);
		return procinsMapper.delete(record);
	}

	@Override
	public List<Procins> queryAdjustBillByAssigneeId2(String assigneeId) {
		Example example = new Example(Procins.class);
		// example.orderBy("end").desc();
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("assignee_id", assigneeId);
		criteria.andEqualTo("type", 2);
		criteria.andIsNotNull("status"); // 已确认
		criteria.orEqualTo("type", 3);
		criteria.andEqualTo("assignee_id", assigneeId);
		criteria.andIsNotNull("status"); // 已确认
		return procinsMapper.selectByExample(example);
	}

	@Override
	public List<Procins> queryProcinsByBillId2(long billId) {
		// Dragon
		Example example = new Example(Procins.class);
		example.orderBy("sequence").asc(); // 升序
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("bill_id", billId);
		criteria.andNotEqualTo("type", 1);
		return procinsMapper.selectByExample(example);

		// BOB写的
		/*
		 * Procins record = new Procins(); record.setBill_id(billId); return
		 * procinsMapper.select(record);
		 */
	}

	@Override
	public Procins queryProcinsBySquenceAndSchoolIdAndAndBillId(int i, String schoolId, Long billId) {
		Procins record = new Procins();
		record.setSequence(i);
		record.setSchool_id(schoolId);
		record.setBill_id(billId);
		return procinsMapper.selectOne(record);
	}

	@Override
	public List<Procins> queryProcinsByBillId3(Long id) {
		Procins record = new Procins();
		record.setBill_id(id);
		return procinsMapper.select(record);
	}

	@Override
	public int updateProcinsTeacher(Procins procins2) {
		return procinsMapper.updateByPrimaryKey(procins2);
	}
}
